<?php
include_once("../includes/common.php");
include_once("../class/User.class.php");
include_once("../class/Explore.class.php");
require_once("../islogin.php");

if ($isadmin!=1){
    header('Location: /');
    exit('权限不足！');
}

$menuavatar = !$user_info["message"]["avatar"] ? "/data/avatar/default.jpg" : $user_info["message"]["avatar"];
$explore = new Explore();

$nowPage = $_GET['page'] ?? 1;
if($nowPage<0){
    exit('page error');
}
$pageNum = 8;
$totalPage = 1;
$pageName = "全站笔记";
$res = $explore->getAllNotes($nowPage,$pageNum);
$totalPage = $res['totalPages'];

if (isset($_GET['del'])) {
    // 在这里执行删除操作
    $explore->hideNote($_GET['uid'], $_GET['del']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER']; // 获取来路页面的 URL
    header("Location: $referrer"); // 重定向到来路页面
    exit();
}
if (isset($_GET['recover'])) {
    // 在这里执行删除操作
    $explore->recoverNote($_GET['recover']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER']; // 获取来路页面的 URL
    header("Location: $referrer"); // 重定向到来路页面
    exit();
}

if (isset($_GET['realdel'])) {
    // 在这里执行删除操作
    $explore->deleteNote($_GET['realdel']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER'];
    header("Location: $referrer");
    exit();
}
if (isset($_GET['status'])) {
    // 在这里执行删除操作
    $explore->upNoteCheckResult($_GET['nid'],$_GET['status']);
    // 删除成功后，跳转回来路页面
    $referrer = $_SERVER['HTTP_REFERER'];
    header("Location: $referrer");
    exit();
}
if (isset($_GET['admin_edit'])) {
    $Temp_u_token = json_decode($user->TempUidlogin($_GET['uid']),true)['message'];

    setcookie("save_me_token", $_COOKIE['admin_token'], time() + (86400 * COOKIE_OVERDUE_DAY), "/");
    setcookie("u_token", $Temp_u_token, time() + (86400 * COOKIE_OVERDUE_DAY), "/");

    header("Location: /control/edit.php?nid=".$_GET['admin_edit']);
    exit();
}

if($_GET['so']){
    $searchKeyword = $_GET['so'] ?? ''; // 获取搜索关键词
    $totalPage = 1;
    $sodataArray = $explore->getAllNotes(1,1000);
    $filteredResults = array_filter($sodataArray['note'], function($item) use ($searchKeyword) {
        return stripos($item['title'], $searchKeyword) !== false; // 不区分大小写的模糊匹配
    });
    //var_dump($filteredResults);exit;
    $filteredResults = array_values($filteredResults);  //编制索引
    $res['note'] = $filteredResults;


}


require_once('page.php');
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <?php require_once('head.php'); ?>
    <style>
        .text-del-line{
            text-decoration: line-through;
        }
    </style>
</head>
<body>
    
<!--==================== Preloader Start ====================-->
  <div class="preloader">
    <div class="loader"></div>
  </div>
<!--==================== Preloader End ====================-->

<!--==================== Sidebar Overlay End ====================-->
<div class="side-overlay"></div>
<!--==================== Sidebar Overlay End ====================-->

<?php require_once('menu.php'); ?>

    <div class="dashboard-main-wrapper">
        <?php require_once('top_navbar.php'); ?>
        <div class="dashboard-body">

            <!-- Recommended Start -->
            <div class="card">
                <div class="card-body">

                    <div class="mb-20 flex-between flex-wrap gap-8">
                        <h4 class="mb-0"><?php echo $pageName;?></h4>

                        <h4 class="mb-0">共<?php echo $res['totalCount'];?>条数据 <?php echo $nowPage?>/<?php echo $totalPage?> 页</h4>

                    </div>

                    <div class="row g-20">
                        <?php for ($i = 0; $i < count($res["note"]); $i++) { ?>
                            <div class="col-xxl-3 col-lg-4 col-sm-6">
                                <div class="card border border-gray-100">
                                    <div class="card-body p-8">
                                        <a href="/explore.php?id=<?php echo $res["note"][$i]['note_id'];?>" class="bg-main-100 rounded-16 overflow-hidden text-center mb-8 h-164 flex-center">
                                            <img src="<?php echo (getFirstValidImage($res["note"][$i]['image']) ?: '/static/img/default.jpg');?>" alt="Course Image" class="cover-img">
                                        </a>
                                        <div class="p-8">
                                            <?php
                                            $check_result = $res["note"][$i]['check_result'];

                                            switch ($check_result) {
                                                case 0:
                                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-warning-50 text-warning-600 mb-16">审核中</span>';
                                                    break;
                                                case 1:
                                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-success-50 text-success-600 mb-16">审核通过</span>';
                                                    break;
                                                case 2:
                                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-danger-50 text-danger-600 mb-16">笔记违规</span>';
                                                    break;
                                                default:
                                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-secondary-50 text-secondary-600 mb-16">未知状态</span>';
                                                    break;
                                            }
                                            echo $res["note"][$i]['type'] == 1 ? '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-main-50 text-main-600 mb-16">视频</span>' : '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-pink-50 text-pink-600 mb-16">笔记</span>';
                                            ?>

                                            <h5 class="mb-8"><a href="/explore.php?id=<?php echo $res["note"][$i]['note_id'];?>" class="<?php echo $res["note"][$i]['is_view']==1 ? 'hover-text-main-600':'text-danger hover-text-danger-600 text-del-line' ;?>"><?php echo $res["note"][$i]['title'];?></a></h5>

                                            <div class="flex-align gap-8">
                                                <div class="flex-align gap-4">
                                                    <span class="text-sm text-main-600 d-flex"><i class="ph ph-clock"></i></span>
                                                    <span class="text-13 text-gray-600"><?php echo formatNoteTime($res["note"][$i]['note_time']);?></span>
                                                </div>
                                                <div class="flex-align gap-4">
                                                    <span class="text-sm text-main-600 d-flex"><i class="ph ph-chats-circle"></i></span>
                                                    <span class="text-13 text-gray-600"><?php echo $res["note"][$i]['comments_count'];?></span>
                                                </div>
                                            </div>

                                            <div class="flex-align gap-8 flex-wrap mt-20">
                                                <img src="<?php echo '/' . ltrim($res["note"][$i]["avatar"] ?: '/data/avatar/default.jpg', '/'); ?>" class="w-28 h-28 rounded-circle object-fit-cover" alt="User Image">
                                                <div>
                                                    <span class="text-gray-600 text-13">作者 <a href="/profile.php?id=<?php echo $res["note"][$i]['user_id'];?>" class="fw-semibold text-gray-700 hover-text-main-600 hover-text-decoration-underline"><?php echo $res["note"][$i]['nickname'];?></a> </span>
                                                </div>
                                            </div>

                                            <div class="flex-between gap-4 flex-wrap mt-20">
                                                <div class="flex-between gap-4">
                                                    <span class="text-15 fw-bold text-danger-600 d-flex"><i class="ph ph-heart"></i></span>
                                                    <span class="text-13 fw-bold text-gray-600"><?php echo $res["note"][$i]['likes_count'];?></span>
                                                </div>
                                                <div class="flex-between gap-4">
                                                    <span class="text-15 fw-bold text-warning-600 d-flex"><i class="ph ph-star"></i></span>
                                                    <span class="text-13 fw-bold text-gray-600"><?php echo $res["note"][$i]['favorites_count'];?></span>
                                                </div>
                                            </div>
                                            <div class="flex-between gap-4 flex-wrap mt-20">

                                                <a href="?admin_edit=<?php echo $res["note"][$i]['note_id'];?>&uid=<?php echo $res["note"][$i]['user_id'];?>" class="btn btn-outline-main rounded-pill py-3 flex-center ">
                                                    编辑 <span class="d-flex text-xl"><i class="ph ph-pencil-simple"></i></span>
                                                </a>
                                                <a href="/explore.php?id=<?php echo $res["note"][$i]['note_id'];?>" class="btn btn-outline-main rounded-pill py-3 flex-center">
                                                    查看 <span class="d-flex text-xl"><i class="ph ph-arrow-right"></i></span>
                                                </a>
                                            </div>
                                            <div class="flex-between gap-4 flex-wrap mt-20">

                                                <a href="aibot.php?nid=<?php echo $res["note"][$i]['note_id'];?>&num=5" class="btn btn-warning rounded-pill py-3 flex-center ">
                                                    AI评论 <span class="d-flex text-xl"><i class="ph ph-robot"></i></span>
                                                </a>
                                                <a href="note_control.php?nid=<?php echo $res["note"][$i]['note_id'];?>" class="btn bg-warning-600 rounded-pill py-3 flex-center">
                                                    操控数据 <span class="d-flex text-xl"><i class="ph ph-cursor-click"></i></span>
                                                </a>
                                            </div>
                                            <div class="flex-between gap-4 flex-wrap mt-20">
                                                <a onclick="return confirm('确定要<?php echo $res["note"][$i]['is_view']==1 ? '隐藏':'恢复' ;?>这篇笔记吗？');" href="?<?php echo $res["note"][$i]['is_view']==1 ? 'del':'recover' ;?>=<?php echo $res["note"][$i]['note_id'];?>&uid=<?php echo $res["note"][$i]['user_id'];?>" class="btn btn-success rounded-pill py-3 flex-center">
                                                    <?php echo $res["note"][$i]['is_view']==1 ? '隐藏':'恢复' ;?> <?php echo $res["note"][$i]['is_view']==1 ? '<span class="d-flex text-xl"><i class="ph ph-eye-slash"></i></span>':'<span class="d-flex text-xl"><i class="ph ph-hand-eye"></i></span>' ;?>
                                                </a>
                                                <a onclick="return confirm('确定要删除这篇笔记吗？不可恢复！删除后将连其他表的该ID一同删除清空！');" href="?realdel=<?php echo $res["note"][$i]['note_id'];?>" class="btn btn-danger rounded-pill py-3 flex-center">
                                                    <span class="d-flex text-xl"><i class="ph ph-trash"></i></span> 删除
                                                </a>
                                            </div>
                                            <div class="flex-center gap-4 flex-wrap mt-20">
                                                <a href="?nid=<?php echo $res["note"][$i]['note_id'];?>&status=1" class="btn bg-main-two-600 rounded-pill py-3 flex-center">
                                                    修改笔记审核为通过 <span class="d-flex text-xl"><i class="ph ph-eye"></i></span>
                                                </a>
                                                <a href="?nid=<?php echo $res["note"][$i]['note_id'];?>&status=2" class="btn bg-purple-600 rounded-pill py-3 flex-center">
                                                    修改笔记审核为违规 <span class="d-flex text-xl"><i class="ph ph-smiley-x-eyes"></i></span>
                                                </a>
                                                <!--<a href="note_control.php?nid=<?php /*echo $res["note"][$i]['note_id'];*/?>" class="btn bg-warning-600 rounded-pill py-3 flex-center">
                                                    操控笔记数据 <span class="d-flex text-xl"><i class="ph ph-cursor-click"></i></span>
                                                </a>-->
                                            </div>


                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>

                    <?php if(!isset($_GET['so'])){?>

                        <div class="flex-between flex-wrap gap-8 mt-20">
                            <a href="<?php echo $prevPageLink; ?>" class="btn btn-outline-gray rounded-pill py-9 flex-align gap-4">
                                <span class="d-flex text-xl"><i class="ph ph-arrow-left"></i></span>
                                上一页
                            </a>

                            <?php if($totalPage>10){?>
                                <ul class="pagination flex-align flex-wrap">
                                    <?php include 'pagination.php'; ?>
                                </ul>
                            <?php }?>

                            <a href="<?php echo $nextPageLink; ?>" class="btn btn-outline-main rounded-pill py-9 flex-align gap-4">
                                下一页 <span class="d-flex text-xl"><i class="ph ph-arrow-right"></i></span>
                            </a>
                        </div>
                    <?php }?>

                </div>
            </div>
            <!-- Recommended End -->
        </div>
        <?php require_once('footer.php'); ?>
    </div>


<?php require_once('foot.php'); ?>


    </body>
</html>